import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yinsh
 * Date: 2025-02-21
 * Time: 10:18
 */
public class Test {
    public void qSort (int[] array) {
        qSortFunc(array,0,array.length-1);
    }

    private void qSortFunc (int[] array,int left,int right) {
        int pivot = partition (array,left,right);
        while (left < right) {
            qSortFunc(array,left,pivot);
            left = pivot + 1;
            pivot = right;
        }
    }

    private int partition (int[] array,int left,int right) {
        int mid = (left+right)/2;
        if (array[left] > array[right]) {
            swap(array,left,right);
        }
        if (array[mid] > array[right]) {
            swap (array,mid,right);
        }
        if (array[left] < array[mid]) {
            swap(array,left,mid);
        }
        int key = array[left];

        while (left < right) {
            while (left < right && array[right] >= key) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= key) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = key;
        return left;
    }

    private void swap (int[] array,int x,int y) {
        int tamp = array[x];
        array[x] = array[y];
        array[y] = tamp;
    }
}
